package com.core.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.core.base.dao.BaseHibernateDao;
import com.core.model.Developer;

/**
 * 开发商DAO
 * 
 * @author pt-chenqw
 *
 */
@Repository("developerDAO")
public class DeveloperDAO extends BaseHibernateDao<Developer> {
	
	/**
	 * 查询所有开发商列表
	 * @return
	 */
	public List<Developer> getAllDevelopers(){
		String hql = "from Developer where status = 1";
		return findAll(hql);
	}
	
	/**
	 * 根据ID查询开发商
	 * @param ID
	 * @return
	 */
	public Developer getDeveloperByID(Integer ID){
		String hql = "from Developer where id = ?";
		return findOne(hql, ID);
	}
	
	/**
	 * 新增开发商
	 * @param developer
	 */
	public void addDeveloper(Developer developer){
		developer.setCreateTime(new Date());
		developer.setUpdateTime(new Date());
		save(developer);
	}
	
	/**
	 * 更新开发商信息
	 * @param developer
	 */
	public void updateDeveloper(Developer developer){
		Developer developerUpdate = getDeveloperByID(developer.getId());
		developerUpdate.setDeveloperName(developer.getDeveloperName());
		developerUpdate.setCeoName(developer.getCeoName());
		developerUpdate.setPhone(developer.getPhone());
		developerUpdate.setFax(developer.getFax());
		developerUpdate.setContactPersonName(developer.getContactPersonName());
		developerUpdate.setWebsite(developer.getWebsite());
		developerUpdate.setEmployees(developer.getEmployees());
		developerUpdate.setOperationArea(developer.getOperationArea());
		developerUpdate.setRemark(developer.getRemark());
		developerUpdate.setStatus(developer.getStatus());
		developerUpdate.setUpdateTime(new Date());
		update(developerUpdate);
	}
	
	/**
	 * 删除开发商,执行HQL删除
	 * @param id
	 */
	public void deleteDeveloperByIDForHQL(Integer id){
		String hql = "delete from Developer where id = ?";
		executeHQL(hql, id);
	}
	
	/**
	 * 使用Hibernate的delete删除
	 * @param id
	 */
	public void deleteDeveloperByIDForDelete(Integer id){
		Developer developer = getDeveloperByID(id);
		delete(developer);
	}

}
